package com.lianbang.staff.module.user

import android.os.Bundle
import android.view.View
import cn.ubest.mvvmcore.base.BaseFragment
import cn.ubest.mvvmcore.utils.DoubleClickHelper
import cn.ubest.mvvmcore.utils.ToastUtils
import com.lianbang.staff.R
import com.lianbang.staff.databinding.FragmentSetPasswordBinding
import com.lianbang.staff.dialog.FindPasswordDialog
import com.lianbang.staff.vm.UserVM
import kotlinx.android.synthetic.main.fragment_set_password.*
import kotlinx.android.synthetic.main.fragment_verify_code.btn_submit

/**
 * Created by maoqi on 2021/1/12.
 */
class SetPasswordFragment : BaseFragment<UserVM, FragmentSetPasswordBinding>() {

    companion object {
        fun newInstance(mobile: String, authToken: String): SetPasswordFragment {
            val args = Bundle()
            val fragment = SetPasswordFragment()
            args.putString("authToken", authToken)
            args.putString("mobile", mobile)
            fragment.arguments = args
            return fragment
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_set_password
    }

    override fun initData(arguments: Bundle?) {

    }

    override fun initView(view: View) {
        btn_submit.setOnClickListener(object : DoubleClickHelper.OnClickListener() {
            override fun onClicked(view: View?) {
                val password = et_password.text.toString()
                val confirmPassword = et_confirm_password.text.toString()
                if (password.trim().isEmpty()) {
                    ToastUtils.toast("密码不能为空")
                    return
                }

                if (confirmPassword.trim() != password.trim()) {
                    ToastUtils.toast("两次输入的密码不同")
                    return
                }

                mViewModel.findPassword(
                    arguments?.getString("mobile") ?: "",
                    arguments?.getString("authToken") ?: "",
                    password,
                    confirmPassword
                ).observe(this@SetPasswordFragment, getObserver {
                    ToastUtils.toast("密码重置成功")
                    if (activity is FindPasswordActivity) {
                        (activity as FindPasswordActivity).addFragment(FindPasswordDialog.newInstance())
                    } else {
                        activity?.finish()
                    }
                })
            }
        })
    }

    override fun loadData() {

    }

}